package gstudio.mp3;

import java.io.File;

import gan.mp3.jmp123.gui.PlayList;
import gan.mp3.jmp123.gui.PlayListThread;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class MiniPlayerComposite extends Composite {
	public static void main(String args[]) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());

		MiniPlayerComposite playerComposite = new MiniPlayerComposite(shell,
				SWT.NONE);
		playerComposite.setInput("d:/t.mp3", false);
		playerComposite.run();
		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();

	}

	private Composite mainComp, subComp1;
	private Button playButton, stopButton;
	private PlayListThread playListThread;
	private int status = 0;
	private Mp3RuntimeWriteByte audio;
	private PlayList list;
	private Label label;

	// 谁使用这个播放器
	private Object parentObject;

	private String inputPath, ouputDir, ouputPreName;

	public String getOuputDir() {
		return ouputDir;
	}

	public void setOuputDir(String ouputDir) {
		this.ouputDir = ouputDir;
	}

	public String getOuputPreName() {
		return ouputPreName;
	}

	public void setOuputPreName(String ouputPreName) {
		this.ouputPreName = ouputPreName;
	}

	boolean writeMp3out;

	public MiniPlayerComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout());
		initGUI();
	}

	private void addActions() {
		playButton.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				doPlay();

			}
		});

		stopButton.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				doStop();
			}
		});

	}

	public void doPlay() {
		if (playListThread == null) {
			audio.reloadInput();
			playListThread = new PlayListThread(list, audio);
			list.append("", inputPath);

		}
		if (!playListThread.isAlive()) {
			playListThread.start();
			status = 1;
			playButton.setText(" Pause ");
			stopButton.setEnabled(true);
		} else if (status == 1) {
			status = 2;
			playListThread.pause();
			playButton.setText(" Play ");
		} else if (status == 2) {
			playListThread.pause();
			status = 1;
			stopButton.setEnabled(true);
			playButton.setText(" Pause ");
		}
	}

	public void doStop() {
		if (playListThread != null && playListThread.isAlive()) {
			playListThread.interrupt();
			playListThread = null;
			status = 1;
		}
		stopButton.setEnabled(false);
		playButton.setText(" Play ");
	}

	public String getInputPath() {
		return inputPath;
	}

	public Button getPlayButton() {
		return playButton;
	}

	public Button getStopButton() {
		return stopButton;
	}

	private void initGUI() {
		mainComp = new Composite(this, SWT.NONE);
		{
			GridLayout gl = new GridLayout(5, false);
			gl.marginHeight = 0;
			gl.marginWidth = 0;
			mainComp.setLayout(gl);
		}
		subComp1 = new Composite(mainComp, SWT.NONE);
		{
			GridData gd = new GridData();
			gd.widthHint = 200;
			subComp1.setLayoutData(gd);
		}

		subComp1.setLayout(new GridLayout(3, false));
		playButton = new Button(subComp1, SWT.NONE);
		playButton.setText(" Play ");
		stopButton = new Button(subComp1, SWT.NONE);
		stopButton.setText(" Stop ");
		stopButton.setEnabled(false);
		label = new Label(subComp1, SWT.BORDER);
		{
			GridData gd = new GridData();
			gd.widthHint = 80;
			label.setLayoutData(gd);
		}

		mainComp.addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent arg0) {
				if (playListThread != null && playListThread.isAlive()) {
					System.exit(0);
				}
			}
		});
		mainComp.pack();
	}

	public void run() {
		File file = new File(inputPath);
		if (!file.exists()) {
			playButton.setEnabled(false);
			return;
		}
		list = new PlayList();
		audio = new Mp3RuntimeWriteByte(41000, label, this, writeMp3out);
		addActions();
	}

	public void setInput(String inputPath, boolean writeMp3out) {
		this.inputPath = inputPath;
		this.writeMp3out = writeMp3out;
	}

	public void setParentObject(Object parentObject) {
		this.parentObject = parentObject;
	}

	public void addSatzComp(final int _startID, final int endID,
			final String inputMp3Path, final String outputMp3Name) {

		if (parentObject instanceof Mp3CutTableComposite) {
			((Mp3CutTableComposite) parentObject).addSatzComp(_startID, endID,
					inputMp3Path, outputMp3Name);
		}
	}

}
